<?php

namespace App\Http\Controllers\Home;

use App\Models\Dining;
use App\Models\DiningReserve;
use App\Models\ProdCategory;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * @group 前台接口文档
 * @title 预定餐桌模块接口
 * @author chenke
 * @description 酒庄前台预定餐桌板块接口
 * @header name:Lang type:string require:1 desc:语言类型(en或zn)
 *  */
class Dini extends Base
{
    /**
     * @title 餐桌分页列表
     * @description 餐桌分页列表
     * @url /api/dini/dinings
     * @method GET
     * @module 餐桌板块
     * @author chenke
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @param name:searchdate type:date require:1 default: other: desc:餐桌预定日期
     * @param name:timetype type:int require:1 default: other: desc:就餐时间段(1,2,3,4四个时间段)
     * @return code:错误码
     * @return message:错误提示
     * @return data:餐桌信息@
     * @data id:餐桌ID title:餐桌英文名 title_zn:餐桌中文名 image:餐桌主图 sitnum:餐桌可坐人数
     */
    public function dinings(Request $request){
        $lang = $request->header('Lang')?$request->header('Lang'):'zn';
        try {
            $data = $request->all();
            $uid = (isset($data['uid'])&&$data['uid'])?$data['uid']:0;
            $limit = $data['limit'];
            $page = $data['page'];
            $where = [
                ['timetype','=',$data['timetype']],['sitday','=',$data['searchdate']]
            ];
            if($uid){
                $where[]=['uid','=',$uid];
            }
            $ids = (new DiningReserve())->where($where)->whereIn('status',[1,2,3])->pluck('id');
            $model = (new Dining())->whereNotIn('id',$ids);
            $offset = ($page-1) * $limit;
            $total =  $model->count();  //获取模型总数据条数
            $result = $model->offset($offset)->limit($limit)->get()->toArray();
            $data = array(
                'total'=>$total,
                'list'=>$result
            );
            return response()->json( [
                'code'=>0,
                'data'=>$data
            ]);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$e->getMessage() //$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 获取用户预定的餐桌
     * @description 餐桌分页列表
     * @url /api/dini/userdini
     * @method GET
     * @module 餐桌板块
     * @author chenke
     * @header name:Token type:string require:1 desc:登录令牌
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @param name:status type:int require:1 default:-1 other: desc:餐桌状态(-1全部0待付款1待接单2待进餐3待结账4已结账5已退订6取消订单)
     * @param name:searchdate type:date require:0 default: other: desc:餐桌预定日期
     * @param name:timetype type:int require:0 default: other: desc:就餐时间段(1,2,3,4四个时间段)
     * @return code:错误码
     * @return message:错误提示
     * @return data:预定信息@
     * @data id:餐桌预定ID tid:餐桌ID title:餐桌英文名 title_zn:餐桌中文名 image:餐桌主图 sitnum:餐桌可坐人数 sitday:预定日期 pernum:预订人数 uid:用户ID status:预定状态 ordersn:订单编号 earnest:订金 total:应付总金额 ordtype(预定类型1预定2现场点单)
     */
    public function userDinings(Request $request){
        $lang = $request->header('Lang')?$request->header('Lang'):'zn';
        $token = $request->header('Token')?$request->header('Token'):'';
        try {
            $data = $request->all();
            $uid = (new User())->where('token',$token)->value('id');
            if(!$uid){
                return response()->json([
                    'code'=>50000,
                    'message'=>$this->lang['LOGIN_EXPIRE'][$lang]
                ]);
            }
            $where[]=['uid','=',$uid];
            $searchdate = (isset($data['searchdate'])&&$data['searchdate'])?$data['searchdate']:'';
            $timetype = (isset($data['timetype'])&&$data['timetype'])?$data['timetype']:'';
            $status = isset($data['status'])?$data['status']:-1;
            $limit = $data['limit'];
            $page = $data['page'];
            if($searchdate){
                $where[] = ['sitday','=',$data['searchdate']];
            }
            if($timetype){
                $where[] = ['timetype','=',$data['timetype']];
            }
            if($status>-1){
                $where[] = ['status','=',$data['status']];
            }

            $model = (new DiningReserve())->leftJoin('dinings','dining_reserves.tid','=','dinings.id')->where($where);
            $model = $model->select('dining_reserves.*','dinings.title','dinings.title_zn','dinings.image','dinings.sitnum');
            $offset = ($page-1) * $limit;
            $total =  $model->count();  //获取模型总数据条数
            $result = $model->offset($offset)->limit($limit)->get()->toArray();
            $data = array(
                'total'=>$total,
                'list'=>$result
            );
            return response()->json( [
                'code'=>0,
                'data'=>$data
            ]);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 餐桌预订页面数据
     * @description 餐桌预订页面关联数据
     * @url /api/dini/cprods
     * @method GET
     * @module 餐桌板块
     * @author chenke
     * @param name:cids type:array require:0 default: other: desc:商品类型ID组成的数组,如[1],[1,2,3].默认所有分类
     * @return code:错误码
     * @return message:错误提示
     * @return data:分类商品信息@
     * @data id:图片ID title:图片英文名 title_zn:图片中文名 src:图片地址 type:图片类型(同参数) rid:图片关联ID created_at:图片添加时间
     */
    public function cateProducts(Request $request){
        try {
            $token = $request->header('Token')?$request->header('Token'):'';
            $lang = $request->header('Lang')?$request->header('Lang'):'zn';
            $cids = (isset($data['cids'])&&$data['cids'])?$data['cids']:[];
            $result = (new ProdCategory())->getCateProducts($cids,$lang,$token);
            return response()->json($result);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 通过日期、桌子ID判断哪些时间段可预订
     * @description 餐桌预定页面时间段判断
     * @url /api/dini/tabstate
     * @method GET
     * @module 餐桌板块
     * @author chenke
     * @param name:tid type:int require:1 default: other: desc:餐桌ID
     * @param name:sitday type:date require:1 default: other: desc:就餐日期
     * @return code:错误码
     * @return message:错误提示
     * @return data:已被预订的时间段(值在1,2,3,4之中的一个数组)
     */
    public function getTableStatus(Request $request){
        try {
            $lang = $request->header('Lang')?$request->header('Lang'):'zn';
            $data = $request->all();
            $where = [
                ['tid','=',$data['tid']],['sitday','=',$data['sitday']]
            ];
            $types = (new DiningReserve())->where($where)->whereIn('status',[1,2,3])->pluck('timetype');
            return response()->json([
                'code'=>0,
                'data'=>$types
            ]);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$e->$this->lang['ERROR'][$lang]
            ]);
        }
    }
}
